﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class PreferenceProfileDA : IPreferenceProfileDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string PREFERENCE_PROFILE_ID = "@PREFERENCE_PROFILE_ID";
        private string SQL_CHECK_PREFERENCE_PROFILE_ID_UNIQUE = " SELECT COUNT(1) FROM  SYSADM.PREFERENCE_PROFILE WHERE PREFERENCE_PROFILE_ID = @PREFERENCE_PROFILE_ID ";
        private string SQL_DELETE_PREFERENCE_PROFILE = " DELETE FROM  SYSADM.PREFERENCE_PROFILE WHERE PREFERENCE_PROFILE_ID = @PREFERENCE_PROFILE_ID ";
        private string SQL_INSERT_PREFERENCE_PROFILE = " INSERT INTO  SYSADM.PREFERENCE_PROFILE ( PREFERENCE_PROFILE_ID, USER_ID, SYSTEM_CREATED, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @PREFERENCE_PROFILE_ID, @USER_ID, @SYSTEM_CREATED, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_PREFERENCE_PROFILE = " SELECT  ROWID, PREFERENCE_PROFILE_ID, USER_ID, SYSTEM_CREATED, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM   SYSADM.PREFERENCE_PROFILE ";
        private string SQL_UPDATE_PREFERENCE_PROFILE = " UPDATE  SYSADM.PREFERENCE_PROFILE SET USER_ID = @USER_ID, SYSTEM_CREATED = @SYSTEM_CREATED, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE PREFERENCE_PROFILE_ID = @PREFERENCE_PROFILE_ID  ";
        private string SYSTEM_CREATED = "@SYSTEM_CREATED";
        private string UPDATED_BY = "@UPDATED_BY";
        private string USER_ID = "@USER_ID";

        public bool CheckPreferenceProfileIDUnique(string preferenceProfileID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREFERENCE_PROFILE_ID", preferenceProfileID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_PREFERENCE_PROFILE_ID_UNIQUE, paras).ToString() == "1");
        }

        public bool CheckPreferenceProfileIDUnique(DataBase dataBase, DbTransaction tran, string preferenceProfileID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREFERENCE_PROFILE_ID", preferenceProfileID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_PREFERENCE_PROFILE_ID_UNIQUE, paras).ToString() == "1");
        }

        public int DeletePreferenceProfile(string preferenceProfileID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREFERENCE_PROFILE_ID", preferenceProfileID) };
                result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_PREFERENCE_PROFILE, paras);
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeletePreferenceProfile(DataBase dataBase, DbTransaction tran, string preferenceProfileID)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREFERENCE_PROFILE_ID", preferenceProfileID) };
                result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_PREFERENCE_PROFILE, paras);
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetPreferenceProfileByQueryList(List<string> sqlWhere, PreferenceProfileQueryEntity preferenceprofileQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_PREFERENCE_PROFILE.Replace(" SYSADM.", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!preferenceprofileQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, preferenceprofileQuery.CurrentPage, preferenceprofileQuery.PageSize, preferenceprofileQuery.SortField, preferenceprofileQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetPreferenceProfileByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, PreferenceProfileQueryEntity preferenceprofileQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_PREFERENCE_PROFILE.Replace(" SYSADM.", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!preferenceprofileQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, preferenceprofileQuery.CurrentPage, preferenceprofileQuery.PageSize, preferenceprofileQuery.SortField, preferenceprofileQuery.SortDirection);
            }
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public PreferenceProfileInfo GetPreferenceProfileByUserID(string preferenceID)
        {
            string sql = this.SQL_SELECT_ALL_PREFERENCE_PROFILE + " WHERE PREFERENCE_PROFILE_ID = @PREFERENCE_PROFILE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREFERENCE_PROFILE_ID", preferenceID) };
            PreferenceProfileInfo preferenceProfileInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    preferenceProfileInfo = new PreferenceProfileInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["PREFERENCE_PROFILE_ID"].ToString(), reader["USER_ID"].ToString(), reader["SYSTEM_CREATED"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return preferenceProfileInfo;
        }

        public int InsertPreferenceProfile(PreferenceProfileInfo preferenceProfileInfo)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                SqlParameter[] paras = this.Set_PreferenceProfile_Parameters(preferenceProfileInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_PREFERENCE_PROFILE, paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertPreferenceProfile(DataBase dataBase, DbTransaction tran, PreferenceProfileInfo preferenceProfileInfo)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = this.Set_PreferenceProfile_Parameters(preferenceProfileInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_PREFERENCE_PROFILE, paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_PreferenceProfile_Parameters(PreferenceProfileInfo preferenceProfileInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.PREFERENCE_PROFILE_ID, SqlDbType.VarChar, 50), new SqlParameter(this.USER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.SYSTEM_CREATED, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            paramArray[0].Value = preferenceProfileInfo.PreferenceProfileID;
            if (!string.IsNullOrEmpty(preferenceProfileInfo.UserID))
            {
                paramArray[1].Value = preferenceProfileInfo.UserID;
            }
            else
            {
                paramArray[1].Value = DBNull.Value;
            }
            paramArray[2].Value = preferenceProfileInfo.SystemCreated;
            paramArray[3].Value = preferenceProfileInfo.CreatedBy;
            if (!string.IsNullOrEmpty(preferenceProfileInfo.UpdatedBy))
            {
                paramArray[4].Value = preferenceProfileInfo.UpdatedBy;
                return paramArray;
            }
            paramArray[4].Value = DBNull.Value;
            return paramArray;
        }
    }
}

